﻿using System;
using TrafficArchives.DataAccess;

namespace TrafficArchives.ValueObjects
{
    public partial class AddValueObject : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Initlize();
            }
        }

        private void Initlize()
        {
            var dbClass = new ValueObjectClassDb();
            var classData = dbClass.GetData();
            DdlValueObjectClass.DataSource = classData;
            DdlValueObjectClass.DataTextField = "Name";
            DdlValueObjectClass.DataValueField = "valueobjectclass_id";
            DdlValueObjectClass.DataBind();

            if (Request.QueryString["action"] == "edit"
                && !string.IsNullOrEmpty(Request.QueryString["id"]))
            {
                LblTitle.InnerText = "修改值对象";
                
                var id = Request.QueryString["id"];
                var db = new ValueObjectDb();
                var data = db.GetData(id);

                if (data.Rows.Count > 0)
                {
                    TxtCode.Text = data.Rows[0]["CODE"].ToString();
                    TxtName.Text = data.Rows[0]["NAME"].ToString();
                    TxtRemark.Text = data.Rows[0]["remark"].ToString();

                    var classId = data.Rows[0]["valueobjectclass_id"].ToString();
                    if (DdlValueObjectClass.Items.FindByValue(classId) != null)
                    {
                        DdlValueObjectClass.SelectedValue = data.Rows[0]["valueobjectclass_id"].ToString();
                    }
                }
            }
            else
            {
                LblTitle.InnerText = "添加值对象";
                if (classData.Rows.Count > 0)
                {
                    DdlValueObjectClass.SelectedValue = classData.Rows[0]["valueobjectclass_id"].ToString();
                }
            }
        }

        protected void BtnSaveClick(object sender, EventArgs e)
        {
            var db = new ValueObjectDb();
            var isEdit = Request.QueryString["action"] == "edit" && !string.IsNullOrEmpty(Request.QueryString["id"]);
            var data = isEdit ? db.GetData(Request.QueryString["id"]) : db.GetNoneData();
            if (string.IsNullOrEmpty(DdlValueObjectClass.SelectedValue))
            {
                LblClassMsg.Visible = true;
                UpdatePanelForm.Update();
                return;
            }
            if (!isEdit)
            {
                var newRow = data.NewRow();
                newRow["valueobject_id"] = Guid.NewGuid().ToString();
                data.Rows.Add(newRow);
            }
            data.Rows[0]["valueobjectclass_id"] = DdlValueObjectClass.SelectedValue;
            data.Rows[0]["code"] = TxtCode.Text;
            data.Rows[0]["name"] = TxtName.Text;
            data.Rows[0]["remark"] = TxtRemark.Text;

            if (isEdit)
            {
                db.Update(data);
            }
            else
            {
                db.Add(data);
            }
            Response.Redirect("ValueObjectView.aspx");
        }
    }
}